// Vue
import Vue from "vue";
import App from "./App";

// 配置
import Setting from "./setting";

// 混合
import mixinApp from "@/mixins/app";

// 插件
import plugins from "@/plugins";

// store
import store from "@/store/index";

// iView 和 iView Pro
import ViewUI from "view-design";
import iViewPro from "@/libs/iview-pro/iview-pro.min.js";

import iIconSelect from "@/components/icon-selector/icon-select.min";
Vue.use(iIconSelect);

//图片验证
import SlideVerify from "vue-monoplasty-slide-verify";
Vue.use(SlideVerify);

import PerfectScrollbar from 'vue2-perfect-scrollbar'
import 'vue2-perfect-scrollbar/dist/vue2-perfect-scrollbar.css'
Vue.use(PerfectScrollbar)

// 菜单和路由
import router from "./router";
import menuHeader from "@/menu/header";
import menuSider from "@/menu/sider";
import { frameInRoutes } from "@/router/routes";

// 多语言
import i18n from "@/i18n";

// 方法
import { getHeaderName, getMenuSider, getSiderSubmenu } from "@/libs/system";

// 内置组件
import iLink from "@/components/link";
import iFrame from "@/components/frame";
import MuseComponents from './components/muse_theme/muse_global_components';
MuseComponents(Vue);


// 使用样式，修改主题可以在 styles 目录下创建新的主题包并修改 iView 默认的 less 变量
// 参考 https://www.iviewui.com/docs/guide/theme
import "./styles/index.less";
import "./libs/iview-pro/iview-pro.css";

import "./assets/iconfont/demo.css";
import "./assets/iconfont/iconfont.css";

import "./assets/muse_theme/iconfont/iconfont.css"; //muse theme
import './assets/muse_theme/less/index.less'; //muse theme
import './assets/muse_theme/font/font.css'; //muse theme

import regulars from './validRule/commonRegulars'; //muse regexp
Vue.prototype.$Regulars = regulars;

import BasicLayout from "@/layouts/basic-layout";

if (window) window.$t = (key, value) => i18n.t(key, value);

import * as filters from "./filter/index";
// 加载所有全局过滤器
Object.keys(filters).forEach((key) => {
  Vue.filter(key, filters[key]);
});

Vue.use(plugins);

Vue.use(ViewUI, {
  i18n: (key, value) => i18n.t(key, value),
});
Vue.use(iViewPro);
Vue.component("i-link", iLink);
Vue.component("i-frame", iFrame);

import ImageViewer from "vue2-viewer";
Vue.use(ImageViewer);

import routesMenu from "@/menu/modules/menu.js";

new Vue({
  mixins: [mixinApp],
  router,
  store,
  i18n,
  render: (h) => h(App),
  created() {
    // 处理路由 得到每一级的路由设置
    this.$store.commit("admin/page/init", frameInRoutes);
    // 设置顶栏菜单
    this.$store.commit("admin/menu/setHeader", menuHeader);
    // 加载用户登录的数据
    this.$store.dispatch("admin/account/load");
    // 初始化全屏监听
    this.$store.dispatch("admin/layout/listenFullscreen");
  },
  watch: {
    // 监听路由 控制侧边栏显示 标记当前顶栏菜单（如需要）
    $route(to, from) {
      let path = to.matched[to.matched.length - 1].path;
      if (!Setting.dynamicSiderMenu) {
        let headerName = getHeaderName(path, menuSider);
        if (headerName === null) {
          path = to.path;
          headerName = getHeaderName(path, menuSider);
        }
        // 在 404 时，是没有 headerName 的
        if (headerName !== null) {
          this.$store.commit("admin/menu/setHeaderName", headerName);
          this.$store.commit("admin/menu/setMenuSider", menuSider);

          const filterMenuSider = getMenuSider(menuSider, headerName);
          this.$store.commit("admin/menu/setSider", filterMenuSider);
          this.$store.commit("admin/menu/setActivePath", to.path);

          const openNames = getSiderSubmenu(path, menuSider);
          this.$store.commit("admin/menu/setOpenNames", openNames);
        }
      } else {
        let meta = {
          auth: true,
        };

        let gd = [
          {
            path: "/home",
            title: "首页",
            header: "home",
            custom: "icon-home iconfont",
          },
          // {
          //   path: "/pageTest",
          //   title: "组件页面",
          //   header: "home",
          //   custom: "icon-home iconfont",
          // },
          // {
          //   path: "/enterpriseInforManage",
          //   title: "企业信息管理",
          //   header: "home",
          //   custom: "icon-company iconfont",
          //   children: [
          //     {
          //       path: "/enterpriseInforInquiry",
          //       title: "企业信息查询",
          //       header: "home",
          //       custom: "",
          //     },
          //     {
          //       path: "/enterpriseInforExamine",
          //       title: "企业信息审核",
          //       header: "home",
          //       custom: "",
          //     },
          //     {
          //       path: "/recordInforInquiry",
          //       title: "备案项目查询",
          //       header: "home",
          //       custom: "",
          //     },
          //     {
          //       path: "/sharingRuleAudit",
          //       title: "企业收益规则审核",
          //       header: "home",
          //       custom: "",
          //     },
          //   ],
          // },
          // {
          //   path: "/enterpriseManage",
          //   title: "企业配置与管理",
          //   header: "home",
          //   custom: "icon-company iconfont",
          //   children: [
          //     {
          //       path: "/organizationManage",
          //       title: "组织及人员管理",
          //       header: "home",
          //       custom: "",
          //     },
          //     {
          //       path: "/myEnterprise",
          //       title: "我的企业",
          //       header: "home",
          //       custom: "",
          //     },
          //     {
          //       path: "/projectFiling",
          //       title: "线下项目备案",
          //       header: "home",
          //       custom: "",
          //     },
          //   ],
          // },
          // {
          //   path: "/businessInformationManage",
          //   title: "经营信息管理",
          //   header: "home",
          //   custom: "icon-jingyingyulan1 iconfont",
          //   children: [
          //     {
          //       path: "/publishBusinessInfo",
          //       title: "发布经营信息",
          //       header: "home",
          //       custom: "",
          //     },
          //     {
          //       path: "/myBusinessInfo",
          //       title: "我的经营信息查询",
          //       header: "home",
          //       custom: "",
          //     },
          //   ],
          // },
          // {
          //   path: "/businessInformationTracking",
          //   title: "经营信息跟踪",
          //   header: "home",
          //   custom: "icon-info iconfont",
          //   children: [
          //     {
          //       path: "/businessInformationReceive",
          //       title: "经营信息接收",
          //       header: "home",
          //       custom: "",
          //     },
          //     {
          //       path: "/businessInformationTrack",
          //       title: "经营信息跟踪",
          //       header: "home",
          //       custom: "",
          //     },
          //     {
          //       path: "/businessInformationWarning",
          //       title: "经营信息预警查询",
          //       header: "home",
          //       custom: "",
          //     },
          //   ],
          // },
          // {
          //   path: "/businessInformationMap",
          //   title: "经营信息一张图",
          //   header: "home",
          //   custom: "icon-data iconfont",
          // },
          // {
          //   path: "/systemManage",
          //   title: "系统配置",
          //   header: "home",
          //   custom: "icon-setting_01 iconfont",
          //   children: [
          //     {
          //       path: "/menuManage",
          //       title: "菜单管理",
          //       header: "home",
          //       custom: "",
          //     },
          //     {
          //       path: "/permissionManage",
          //       title: "权限管理",
          //       header: "home",
          //       custom: "",
          //     },
          //     // {
          //     //   path: "/roleManage",
          //     //   title: "组织管理",
          //     //   header: "home",
          //     //   custom: "",
          //     // },
          //     {
          //       path: "/dictionaryManage",
          //       title: "字典管理",
          //       header: "home",
          //       custom: "",
          //     },
          //     {
          //       path: "/logManage",
          //       title: "日志管理",
          //       header: "home",
          //       custom: "",
          //     },
          //     {
          //       path: "/processManage",
          //       title: "流程管理",
          //       header: "home",
          //       custom: "",
          //     },
          //     {
          //       path: "/warningTimeSet",
          //       title: "预警时间设置",
          //       header: "home",
          //       custom: "",
          //     },
          //   ],
          // },
          // {
          //   path: `/console`,
          //   title: "主控台",
          //   header: "home",
          //   custom: "i-icon i-icon-notification",
          // },
          // {
          //   path: "/log",
          //   header: "home",
          //   title: "日志",
          //   icon: "md-locate",
          // },
        ];

        let dataMenuRole = JSON.parse(localStorage.getItem("userMenu")); // 存储菜单

        let menuDataList = [];
        let routeDataList = [];

        const loadView = (view) => {
          // 路由懒加载
          return (resolve) => require([`@${view}`], resolve);
        };

        const routeFilte = (list) => {
          list.forEach((o) => {
            if (o.level == 1) {
              o.path = "/";
              o.name = o.routeName;
              o.redirect = {
                name: o.children ? o.children[0].routeName : o.routeName,
              };
              o.meta = meta;
              o.component = BasicLayout;
            }
            if (o.level != 1) {
              o.path = o.routePath;
              o.name = o.routeName;
              o.meta = {
                ...meta,
                title: o.menuName,
                closable: true,
              };
              o.component = loadView(o.componentPath);
            }
            if (o.children) {
              routeFilte(o.children);
            }
          });
        };

        // routeFilte(dataMenuRole);

        console.log("rl", dataMenuRole);
        const menuFilte = (data) => {
          for (let item in data) {
            data[item].path = data[item].routePath;
            data[item].title = data[item].menuName;
            data[item].header = "home";
            data[item].custom =
              data[item].icon != "ivu-icon-ios-disc"
                ? " iconfont " + data[item].icon
                : data[item].icon;
            delete data[item].icon;
            if (data[item].childrenMenuList) {
              data[item].children = data[item].childrenMenuList;
              menuFilte(data[item].children);
            }
          }
        };
        menuFilte(dataMenuRole);

        // routes = rl
        menuDataList = dataMenuRole;
        let routes = routesMenu;
        let allRoutes = this.$router.getRoutes();
        // this.$router.addRoutes(routes);
        if (allRoutes.length < 30) {
          this.$router.addRoutes(routes);
        }
        // 完整的 frameInRoutes，更新 vuex 的数据
        const newRoutes = frameInRoutes.concat(routes);
        localStorage.setItem("newRoutes", JSON.stringify(routes));
        this.$store.commit("admin/page/init", newRoutes);
        let dynamicSider = gd.concat(menuDataList);
        console.log(dynamicSider,'dynamicSider',menuDataList)
        let headerName1 = getHeaderName(path, dynamicSider);
        if (headerName1 === null) {
          path = to.path;
          headerName1 = getHeaderName(path, dynamicSider);
        }
        const filterMenuSider = getMenuSider(dynamicSider, headerName1);
        this.$store.commit("admin/menu/setHeaderName", "home");
        this.$store.commit("admin/menu/setMenuSider", dynamicSider);
        this.$store.commit("admin/menu/setSider", filterMenuSider);
        this.$store.commit("admin/menu/setActivePath", to.path);
        const openNames1 = getSiderSubmenu(path, dynamicSider);
        console.log(openNames1, "openNames1");
        this.$store.commit("admin/menu/setOpenNames", openNames1);
      }
      this.appRouteChange(to, from);
    },
  },
}).$mount("#app");
